package com.qcby.controller;

import com.qcby.entity.User;
import com.qcby.service.UserLoginService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Tom
 * @Date: 2021/07/20/11:01
 * @Description:
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserLoginService userLoginService;
    @Autowired
    private HttpSession session;
    @RequestMapping("main")
    public String show(Model model){
      return "user-equip/login";
    }

    @RequestMapping("login")
  public String login(Model model,User user){
      User u = userLoginService.judgeIn(user);
      session.setAttribute("userId",u.getUserId());
      Integer i= Integer.parseInt(session.getAttribute("userId").toString());
      System.out.println(i);
      if (u != null) {
        model.addAttribute("success","登陆成功");
        return "user-equip/auctionhouse";

      }else {
        model.addAttribute("error","账号密码错误");
        return "user-equip/login";
      }
    }
  @RequestMapping("toregister")
  public String toregister(){
    return "user-equip/register";
  }
  @RequestMapping("register")
  public String register(Model model,User user){
      int t = userLoginService.register(user);
      if (t > 0) {
        model.addAttribute("success","恭喜你注册成功");
        return "user-equip/login";
      }else {
        model.addAttribute("error","注册失败");
        return "user-equip/register";
      }
  }
}
